package com.qianxun.module.datasource1.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianxun.framework.common.util.Md5Util;
import com.qianxun.framework.common.util.ObjectUtil;
import com.qianxun.module.datasource1.admin.dto.SysUserDTO;
import com.qianxun.module.datasource1.admin.entity.SysUser;
import com.qianxun.module.datasource1.admin.mapper.SysUserMapper;
import com.qianxun.module.datasource1.admin.service.*;
import com.qianxun.module.base.exception.ServiceInterruptionException;
import com.qianxun.module.datasource1.develop.service.SysFunctionRoleService;
import com.qianxun.plugins.shiro.ShiroUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 后台管理员 服务实现类
 * </p>
 *
 * @author admin
 * @since 2019-11-13
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    private SysUserMenuRoleRelationService sysUserMenuRoleRelationService;
    @Resource
    private SysUserFunctionRoleRelationService sysUserFunctionRoleRelationService;
    @Resource
    private SysFunctionRoleService sysFunctionRoleService;
    @Resource
    private SysDataModifyLockService sysDataModifyLockService;

    @Override
    public void deleteById(Long id) {
        SysUser old = getById(id);
        if(old == null){
            return;
        }
        //删除菜单角色关联关系
        sysUserMenuRoleRelationService.removeByUserId(id);
        //删除功能角色关联关系
        sysUserFunctionRoleRelationService.removeByUserUuid(old.getUuid());

        removeById(id);
    }

    @Override
    public void add(SysUserDTO query) {
        SysUser old = getByUsername(query.getUsername());
        if(old != null){
            throw ServiceInterruptionException.fail(400,"用户名已存在" + query.getUsername());
        }
        //参数整理
        SysUser sysUser = new SysUser();
        ObjectUtil.switchAllFieldValue(sysUser, query);
        sysUser.init();
        //密码加密
        String newPwd = Md5Util.getInstance().saltEncrypt(query.getPassword());
        sysUser.setPassword(newPwd);

        save(sysUser);
    }

    @Override
    public void setPassword(String oldPassword, String password) {
        SysUser sysUser = ShiroUtil.getUser(SysUser.class);;
        boolean passwordChecked = Md5Util.getInstance().saltValid(oldPassword ,sysUser.getPassword());
        if(!passwordChecked){
            throw ServiceInterruptionException.fail(400 ,"旧密码错误");
        }
        String newPassword = Md5Util.getInstance().saltEncrypt(password);
        sysUser.setPassword(newPassword);

        updateById(sysUser);

        //更新数据锁中该管理员的密码，否则数据将无法解锁
        sysDataModifyLockService.updatePasswordByCreateKey(sysUser.getId() + "" ,newPassword);
    }

    @Override
    public SysUser getOneByUsername(String username) {
       return getByUsername(username);
    }

    @Override
    public List<String> getFunctionRoleListByUuid(String uuid) {
        return sysFunctionRoleService.getFunctionRoleNameListByUserUuid(uuid);
    }

    @Override
    public Set<String> getPermissionUrls(long userId) {
        return sysUserMenuRoleRelationService.getPermissionUrl(userId);
    }

    private SysUser getByUsername(String username){
        Wrapper wrapper = new QueryWrapper<SysUser>().lambda().eq(SysUser::getUsername ,username);
        return getOne(wrapper);
    }
}
